<template>
  <div class="aui-card-list">
    <uni-list-query-card :queryShow="queryShow" @query-click="getDataList()" @click="(e)=>{this.queryShow=e}" @searchReset="searchReset()">
      <el-col :span="6">
        <el-form-item :label="$t('sysApp.appName')">
          <el-input v-model="dataForm.appName" :placeholder="$t('sysApp.appName')" clearable></el-input>
        </el-form-item>
      </el-col>
    </uni-list-query-card>
    <el-card shadow="never" class="aui-card-list-body">
      <vxe-toolbar :setting="{immediate:true}">
        <template v-slot:buttons>
          <el-button v-if="$hasPermission('sys:sysApp:save')" type="primary" @click="addOrUpdateHandle()">
            {{ $t('add') }}
          </el-button>
        </template>
        <template v-slot:tools>
          <el-button icon="el-icon-refresh" @click="getDataList()" circle></el-button>
        </template>
      </vxe-toolbar>
      <div class="mod-sys__sysApp">
        <vxe-table :loading="dataListLoading" :data="dataList" ref="xTable" auto-resize class="vxe-table-element" remote-sort @sort-change="dataListSortChangeHandle" @select-change="dataListSelectionChangeHandleVex" @select-all="dataListSelectionAllChangeHandleVex">
          <vxe-table-column type="index" title="序号" width="50" align="center"></vxe-table-column>
          <vxe-table-column field="appId" title="sysApp.appId" width="120"></vxe-table-column>
          <vxe-table-column field="appName" title="sysApp.appName" width="120"></vxe-table-column>
          <vxe-table-column field="appType" title="sysApp.appType" width="100" align="center">
            <template slot-scope="scope">
              <el-tag v-if="scope.row.appType == 0" size="small" type="info">{{ $t('sysApp.appType0') }}</el-tag>
              <el-tag v-else size="small" type="success">{{ $t('sysApp.appType1') }}</el-tag>
            </template>
          </vxe-table-column>
          <vxe-table-column field="apiAuth.accessKey" title="授权码">
          </vxe-table-column>
          <vxe-table-column field="updateDate" title="common.updateDate" width="160">
          </vxe-table-column>
          <vxe-table-column field="status" title="user.status" sortable width="80" align="center">
            <template slot-scope="scope">
              <el-tag v-if="!scope.row.status" size="small" type="danger">{{ $t('sysApp.status0') }}</el-tag>
              <el-tag v-else size="small" type="success">{{ $t('sysApp.status1') }}</el-tag>
            </template>
          </vxe-table-column>
          <vxe-table-column title="handle" width="260">
            <template slot-scope="scope">
              <el-button v-if="$hasPermission('sys:sysApp:update')" type="text" icon="el-icon-edit" size="small" @click="addOrUpdateHandle(scope.row.id)">{{ $t('update') }}
              </el-button>
              <el-button v-if="$hasPermission('sys:sysApp:delete')" type="text" icon="el-icon-delete" size="small" @click="deleteHandle(scope.row.id)">{{ $t('delete') }}
              </el-button>
              <el-button v-if="!scope.row.status" type="text" size="small" icon="el-icon-circle-check" @click="updateStatus(scope.row.id,true)">启用
              </el-button>
              <el-button v-if="scope.row.status" type="text" size="small" icon="el-icon-circle-close" @click="updateStatus(scope.row.id,false)">停用
              </el-button>
              <el-button type="text" size="small" icon="el-icon-setting" @click="authConfigHandle(scope.row)">接口授权
              </el-button>
            </template>
          </vxe-table-column>
        </vxe-table>
        <el-pagination :current-page="page" :page-sizes="[10, 20, 50, 100]" :page-size="limit" :total="total" layout="total, sizes, prev, pager, next, jumper" @size-change="pageSizeChangeHandle" @current-change="pageCurrentChangeHandle">
        </el-pagination>
        <!-- 弹窗, 新增 / 修改 -->
        <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
        <!-- 授权窗口 -->
        <authConfig v-if="configVisible" ref="authConfig" @refreshDataList="getDataList"></authConfig>
      </div>
    </el-card>
  </div>
</template>
<script>
import mixinViewModule from '@/mixins/view-module'
import AddOrUpdate from './sysApp-add-or-update'
import AuthConfig from './sysAppApi'

export default {
  mixins: [mixinViewModule],
  data () {
    return {
      mixinViewModuleOptions: {
        getDataListURL: '/sys/sysApp/page',
        getDataListIsPage: true,
        deleteURL: '/sys/sysApp',
        deleteIsBatch: true,
        exportURL: '/sys/sysApp/export'
      },
      configVisible: false,
      dataForm: {
        appName: ''
      }
    }
  },
  methods: {
    authConfigHandle (row) {
      this.configVisible = true
      this.$nextTick(() => {
        this.$refs.authConfig.app = row
        this.$refs.authConfig.init()
      })
    },
    updateStatus (id, status) {
      var title = ''
      if (status) {
        title = ' 确定要停用该应用吗'
      } else {
        title = ' 确定要停用该应用吗'
      }
      var params = {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        lockScroll: false,
        type: 'warning'
      }
      this.$confirm(title, '提示', params)
        .then(() => {
          this.$http['post']('/sys/sysApp/updateStatus', { id, status }).then(({ data: res }) => {
            if (res.code !== 0) {
              return this.$message.error(res.msg)
            }
            this.$message({
              message: this.$t('prompt.success'),
              type: 'success',
              duration: 500,
              onClose: () => {
                this.visible = false
                this.getDataList()
              }
            })
          }).catch(() => { })
        })
    }
  },
  components: {
    AddOrUpdate,
    AuthConfig
  }
}
</script>
